package com.htfyun.ft.fragment.engineer;

import android.os.Bundle;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.os.storage.StorageVolume;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.htfyun.ft.R;
import com.htfyun.ft.fragment.BaseFragment;
import com.htfyun.ft.fragment.factory.FactoryTestItemsEnum;
import com.htfyun.ft.fragment.factory.ResultAttr;
import com.htfyun.ft.utils.DebugLog;
import com.htfyun.hardware.HardDiskEnum;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.Nullable;

/**
 * Created by sst on 2018/7/11.
 */

public class FragmentHardDisk extends BaseFragment
        implements View.OnClickListener {

    private final static String TAG = FragmentHardDisk.class.getSimpleName();

    private TextView txtShowSerial;
    private TextView txtShowTemp;

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_engineer_hard_disk;
    }

    @Override
    protected void onInitView(@Nullable Bundle savedInstanceState) {

        initBtn(R.id.btnGetSerial);
        initBtn(R.id.btnGetTemp);

        txtShowSerial = (TextView) findViewById(R.id.txtShowSerial);
        txtShowTemp = (TextView) findViewById(R.id.txtShowTemp);

    }

    private void initBtn(int resId) {
        View btn = findViewById(resId);
        if (btn != null) {
            btn.setOnClickListener(this);
        }
    }

    @Override
    protected void onVisible() {
        super.onVisible();
        updateSerial();
        updateTemp();
    }

    private void updateSerial() {
        Map<HardDiskEnum, String> map = new HashMap<>();
        for (HardDiskEnum hardDiskEnum : HardDiskEnum.values()) {
            String sn = HardDiskEnum.getSerialNumber(hardDiskEnum);
            if (!TextUtils.isEmpty(sn)) {
                map.put(hardDiskEnum, sn);
            }
        }

        if (map.isEmpty()) {
            txtShowSerial.setText(R.string.engineer_hard_disk_not_found);
        } else {
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<HardDiskEnum, String> entry : map.entrySet()) {
                sb.append("name: " + entry.getKey() + "-> serialNo: " + entry.getValue());
                sb.append("\n");
            }
            txtShowSerial.setText(sb.toString());

        }
    }

    private void updateTemp() {
        StorageManager sm = mContext.getSystemService(StorageManager.class);

        List<StorageVolume> volList = sm.getStorageVolumes();

        txtShowTemp.setText(R.string.engineer_hard_disk_not_found);

        StringBuilder sb = new StringBuilder("");

        for (StorageVolume vol : volList) {
            if (!vol.isPrimary()) {
                int temp = getTemperature(sm, getVolumeId(vol));
                sb.append("path:" + getVolumePath(vol) + ", temp(C):" + temp);
                sb.append("\n");
            }
        }

        if (!TextUtils.isEmpty(sb.toString())) {
            txtShowTemp.setText(sb.toString());
        }
    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();
        switch (v.getId()) {
            case R.id.btnGetSerial:
                updateSerial();

                break;
            case R.id.btnGetTemp:
                updateTemp();
                break;
        }

    }

    public String getVolumePath(StorageVolume volume) {
        try {
            if (volume.getState().equals(Environment.MEDIA_MOUNTED)) {
                Method getPath = StorageVolume.class.getMethod("getPath");
                String path = (String) getPath.invoke(volume);
                DebugLog.e("Storage Path--->" + path);

                return path;

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }

    public String getVolumeId(StorageVolume volume) {

        try {

            Method getId = StorageVolume.class.getMethod("getId");
            String id = (String) getId.invoke(volume);
            DebugLog.e("Storage getId--->" + id);

            return id;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }

    public int getTemperature(StorageManager manager, String volId) {

        try {

            Method getTemperature = StorageManager.class.getMethod("getTemperature", String.class);
            int temp = (int) getTemperature.invoke(manager, volId);
            DebugLog.e("Storage getTemperature--->" + temp);

            return temp;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return -1;
    }

}
